﻿<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle"> </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :scroll="{ x: 1500, y: `calc(100vh - ${tableTop + 222 + 'px'})` }"
        :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <template class="table-operator" slot="operator" v-if="hasPerm('PolicyStep:add')">
          <a-button type="primary" v-if="hasPerm('PolicyStep:add')" icon="plus" @click="$refs.addForm.add(taskId)"
            >新增任务步骤</a-button
          >
        </template>
        <span slot="actionTypescopedSlots" slot-scope="text">
          {{ 'ActionType' | enumType(text) }}
        </span>
        <span slot="backupTypescopedSlots" slot-scope="text">
          {{ 'BackupType' | enumType(text) }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('PolicyStep:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('PolicyStep:edit') & hasPerm('PolicyStep:delete')" />
          <a-popconfirm
            v-if="hasPerm('PolicyStep:delete')"
            placement="topRight"
            title="确认删除？"
            @confirm="() => PolicyStepDelete(record)"
          >
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
import { STable } from '@/components'
import { PolicyStepPage, PolicyStepDelete } from '@/api/modular/main/PolicyStepManage'
import addForm from './addForm.vue'
import editForm from './editForm.vue'
export default {
  components: {
    STable,
    addForm,
    editForm,
  },
  props: ['taskId'],
  data() {
    return {
      tableTop: 0,
      queryParam: {},
      columns: [
        {
          title: '标题',
          align: 'center',
          dataIndex: 'title',
          width: 100,
        },
        {
          title: '操作类型',
          align: 'center',
          dataIndex: 'actionType',
          width: 100,
          scopedSlots: { customRender: 'actionTypescopedSlots' },
        },
        {
          title: '步骤序号',
          align: 'center',
          sorter: true,
          dataIndex: 'stepNo',
          width: 100,
        },
        {
          title: '备份类型',
          align: 'center',
          dataIndex: 'backupType',
          width: 100,
          scopedSlots: { customRender: 'backupTypescopedSlots' },
        },
        {
          title: '压缩备份',
          align: 'center',
          width: 100,
          customRender: (vlaue) => (vlaue ? <a-tag color="green">True</a-tag> : <a-tag color="volcano">False</a-tag>),
          dataIndex: 'isCompressBuckup',
        },
        {
          title: '创建远程备份',
          align: 'center',
          width: 100,
          customRender: (vlaue) => (vlaue ? <a-tag color="green">True</a-tag> : <a-tag color="volcano">False</a-tag>),
          dataIndex: 'isRemoteCopy',
        },
        {
          title: '摘要说明',
          align: 'center',
          width: 100,
          dataIndex: 'description',
        },
        {
          title: '月备份',
          align: 'center',
          width: 100,
          customRender: (vlaue) => (vlaue ? <a-tag color="green">True</a-tag> : <a-tag color="volcano">False</a-tag>),
          dataIndex: 'isMonthly',
        },
      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        this.queryParam.policyTaskId = this.taskId
        return PolicyStepPage(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
    }
  },
  created() {
    if (this.hasPerm('PolicyStep:edit') || this.hasPerm('PolicyStep:delete')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        align: 'center',
        fixed: 'right',
        scopedSlots: { customRender: 'action' },
      })
    }
  },
  methods: {
    /**
     * 查询参数组装
     */
    switchingDate() {
      const obj = JSON.parse(JSON.stringify(this.queryParam))
      return obj
    },
    refresh(taskId) {
      this.taskId=taskId;
      this.$refs.table.refresh()
    },
    PolicyStepDelete(record) {
      PolicyStepDelete(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败') // + res.message
        }
      })
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
  },
}
</script>
<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
